<?php
	phpinfo();
	exit;
/*print_r($_REQUEST);
?>
<form name="main">
<br /><br />
<input type="radio" name="attivo" value="S" submit/>Si<br />
<input type="radio" name="attivo" value="N" submit/>No<br />
<br />
<input type="button" onClick="cerca()" value="Submit" />
</form>

<script type="text/javascript" >
function cerca()
{
	alert(valueForm(true));
	document.main.submit();
}

function valueForm( tipo )
{
	var objForm = document.forms[0];
	var totElements = objForm.elements.length;
	var retParam = "";
	
	while ( totElements-- )
	{
		typeElement = objForm.elements[totElements].type;
		if ( typeElement == "text" || typeElement == "hidden" || typeElement == "select-one" || typeElement == "checkbox" || typeElement == "textarea")
		{
			if ( objForm.elements[totElements].getAttribute("submit") != null && tipo )
			{
				if ( retParam != "" ) retParam+= "&";
				retParam+= objForm.elements[totElements].name+"="+objForm.elements[totElements].value;
			}
			else if ( objForm.elements[totElements].getAttribute("nosubmit") == null && ! tipo )
			{
				if ( retParam != "" ) retParam+= "&";
				retParam+= objForm.elements[totElements].name+"="+objForm.elements[totElements].value;
			}
		}
		else if ( typeElement == "radio" )
		{
			if ( objForm.elements[totElements].checked ) 
			{
				if ( retParam != "" ) retParam+= "&";
				retParam+= objForm.elements[totElements].name+"="+objForm.elements[totElements].value;
			}
		}
	}
	return retParam;
}
</script>
<?*/

	$connServer = 1;
	$connServerError = 1;
	$checkRead = 0;
	$timesRead = 10;
	$countRead = 10;
	$checkBuffer = 3;
	$timesCheckBuffer = 3;
	$errString = "";
	$errNumber = 0;
	$sockServer;
	$vars = array("rxEOF" => 0);
	$sockServer = false;
	
	function connect($host, $port)
	{
		global $sockServer, $connServer, $connServerError;
		if ( $connServer )
		{
			print "OK 1<br>\n";
			$sockServer = fsockopen($host, $port, $errString, $errNumber, 5);
			if ( $errNumber != 0 )
			{
			print "OK 4<br>\n";
				print ">$errString<<br>\n";
				$connServer = 1;
				$connServerError = 1;
				return true;
			}
			print "OK 2<br>\n";
			$connServer = 0;
			$connServerError = 0;
			stream_set_blocking($sockServer, FALSE);
			print "OK 3<br>\n";
		}
		return false;
	}
	
	function rxData()
	{
		global $sockServer, $connServer, $connServerError;
		global $checkRead, $buf, $countRead, $timesRead, $checkBuffer, $vars;
		$checkRead = 1;
		$buf = "";
		
		print "OK rxData 0<br>\n";
		if ( ($buf = fread($sockServer, 16384)) === FALSE )
		{
			print "OK rxData 1<br>\n";
			$countRead--;
			$checkRead = 0;
		}
		
		if ( ! $countRead )
		{
			print "OK rxData 2<br>\n";
			$countRead = $timesRead;
			$connServer = 1;
			$connServerError = 1;
			return 0;
		}

		if ( $checkRead )
		{
			print "OK rxData 3 16384 > ".strlen($buf)." - $buf<br>\n";
			$countRead = $timesRead;
			
			if ( $buf == "" )
			{
				if ( $checkBuffer )
				{
					print "OK rxData 4<br>\n";
					$checkBuffer--;
					usleep( 50000 );
				}
				else
				{
					print "OK rxData 5<br>\n";
					$vars["rxEOF"] = 0;
					return;
				}				
			}
			else
			{
				if ( preg_match( "/\.EOF$/", $buf ) ) 
				{
					$vars["rxEOF"] = 0;
					$buf = preg_replace("/\.EOF$/","",$buf);
				}
			}
			return $buf;
		}
		else
		{
			print "OK rxData 6<br>\n";
			usleep( 50000 );
		}	
		return "";
	}

	function connError()
	{
		return $connServerError;
	}

	function rxEof()
	{
		global $vars;
		return $vars["rxEOF"];
	}

	function txData($str)
	{
		global $connServer, $connServerError;
		global $sockServer, $vars, $checkBuffer, $timesCheckBuffer;
		
		print "txData $str<br>\n";

		if ( fwrite($sockServer, $str, strlen( $str ) ) === FALSE )
		{
			$errString = "Error : txData";
			return 1;
		}
		
		$vars["rxEOF"] = 1;
		$checkBuffer = $timesCheckBuffer;
		
		usleep( 50000 );
		return 0;
	}

	function reqRemoteResponse($cmd, $timeout = 0.5)
	{
		$response = "";

		if ( $cmd != "" )
		{			
			if ( txData("$cmd\n") )
			{
				return $errString;
			}
			
			//usleep( $timeout * 1000000 );
		}
		$cnt = 0;
		while( rxEof() && ! connError() )
		{
			//$cnt++;
			print "OK reqRemoteResponse $cnt - ".rxEof()."<br>";
			$response.= rxData();
			//if ( $cnt > 10 ) break;
		}
		
		return $response;
	}

	function reqRemoteRows($cmd, &$result)
	{
		$response = "";
		
		if ( $cmd != "" )
		{
			if ( txData("$cmd\n") )
			{
				return $errString;
			}
			
			while( rxEof() && ! connError() )
			{
				$response.= rxData();
				if ( preg_match("/^ERROR/i", $response) ) return $response;
				while ( $end = strpos($response, "\n") )
				{
					$result[] = substr($response, 0, $end);
					$response = substr($response, $end + 1);
				}
				//usleep( 1000000 );
			}
			$result[] = $response;
		}
		return "";
	}

connect("localhost", 3000);
$resp = reqRemoteResponse("login iado|megatron|acme|php");
reqRemoteRows("AGENDA_GIORNO 15/04/2009", $rows);
print_r($rows);


/*$tmp = split("/", "16/01/2009");
$tomorrow  = mktime(0, 0, 0, $tmp[1]  , $tmp[0]+1, $tmp[2]);
print date( "d/m/Y" ,$tomorrow);
print "<br><br>";

$dayDiff = -1;
list($day,$month,$year) = explode("/","10/12/2008");
echo "$day,$month,$year<br>";
$tsDate = mktime ( 0, 0, 0, intval($month), intval($day), intval($year) );
echo "$tsDate<br>";
$tsDay = $dayDiff * (60 * 60 * 24);
echo "$tsDay<br>";
$tsDate += $tsDay;
echo "$tsDate<br>";
list($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($tsDate);
echo "$mday, $mon, $year<br>";
$year += 1900;
$mon += 1;
$dt = "$mday/$mon/$year";
echo date("d/m/Y",$tsDate);*/
?>
